$(document).ready(function() {
	// 出库申请商品的数据绑定
	var outStockMaterialListVue = new Vue({
		el : '#outStockApplyMaterialDiv',
		data : {
			items : null
		},
		methods : {
			deleteIt : function(material, num) {
				openDeleteMaterialConfirm(material, num);
			}
		}
	});
	
	//库存的数据绑定
	var stockMaterialVue = new Vue({
		el : '#stockMaterialDiv',
		data : {
			items : null
		},
		methods : {
			chooseQuantity : function(stock,num) {
				openChooseStockQuantity(stock,num);
			}
		},
		beforeCreate : function() {
			updateStockList();
		}
	});

	// 选择的商品
	var materialList = new Array();
	
	//库存商品
	var stockList=new Array();


	// 打开选择出库数量的输入框
	function openChooseStockQuantity(stock,num) {
		var a = layer.prompt({
			title : '选择'+stock.material.materialName+'数量,库存：' + stock.quantity + ',单位：'
					+ stock.material.unit.unitName
		}, function(quantity, index) {
			if (quantity > stock.quantity) {
				layer.msg('库存不足！请重新输入');
			} else {
				// 从库存list中隐藏已选择的
				stockList.splice(num, 1);
				stockMaterialVue.items = stockList;
				
				var material = {
					"materialId" : stock.materialId,
					"materialName" : stock.material.materialName,
					"materialQuantity" : quantity,
					"unitName" : stock.material.unit.unitName
				};
				materialList.push(material);
				outStockMaterialListVue.items = materialList;
				$("#materialList").val(JSON.stringify(materialList));
				layer.close(index);
			}
		});
	}

	//查询所有的库存
	function updateStockList(){
		$.get(SERVER_PRE_URL + "stock/getAllStockAjax",
				function(data) {
			stockMaterialVue.items = data;
			for ( var i in data) {
				stockList.push(data[i]);
			}
				});
	}

	
	
	//打开删除待出库的商品的确认框
	function openDeleteMaterialConfirm(material, num) {
		layer.confirm('确认删除:' + material.materialName, {
			btn : [ '确认', '取消' ]
		// 按钮
		}, function() {
			layer.msg('删除成功!');
			materialList.splice(num, 1);
			outStockMaterialListVue.items = materialList;
			$("#materialList").val(JSON.stringify(materialList));
		}, function() {

		});
	}


});